package jp.co.google.ben.picasa.client;

import java.util.ArrayList;
import java.util.List;

import jp.co.google.ben.picasa.model.AlbumModel;
import jp.co.google.ben.picasa.model.PhotoModel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyPicasa implements EntryPoint {
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final AlbumServiceAsync albumService = GWT
			.create(AlbumService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		//login
		albumService.loadAlbum(new AsyncCallback<List<AlbumModel>>() {
				public void onSuccess(List<AlbumModel> albumList) {
					// TODO Auto-generated method stub
					List<Widget> panelList = createImagePanelList(albumList);
					RootPanel.get("PicasaContent").add(createGrid(panelList));
				}
				
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
		});
		
	}
	
	
	private List<Widget> createImagePanelList(List<AlbumModel> albumList){
		List<Widget> imagePanelList = new ArrayList<Widget>();
		AlbumModel albumModel = null;
		for(int i=0;i<albumList.size();i++){
			albumModel = albumList.get(i);
			
			Image image = new Image();
			image.setUrl(albumModel.getThumbnailUrl());
			image.setHeight("100");
			image.setWidth("100");
			image.setTitle(albumModel.getAlbumId());
			
			image.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					// TODO Auto-generated method stub
					Image image = (Image)event.getSource();
					albumService.loadPhoto(image.getTitle(),0, new AsyncCallback<List<PhotoModel>>() {
						
						public void onSuccess(List<PhotoModel> photoList) {
							// TODO Auto-generated method stub
							RootPanel.get("PicasaContent").clear();
							
							VerticalPanel vPanel = new VerticalPanel();
							
							Anchor myAlbumAnchor = new Anchor();
							myAlbumAnchor.setHref("/MyPicasa.html");
							myAlbumAnchor.setHTML("My Album");

							
							
							HorizontalPanel topPanel = new HorizontalPanel();
							topPanel.add(myAlbumAnchor);
							
							vPanel.add(topPanel);
							
							List<Widget> panelList = createPhotoImagePanelList(photoList);
							vPanel.add(createGrid(panelList));
							
							RootPanel.get("PicasaContent").add(vPanel);
						}
						
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
					});
				}
			});
			
			VerticalPanel  vPanel  = new VerticalPanel ();
			vPanel.add(image);
			
			vPanel.add(new HTML(albumModel.getAlbumName()));
			
			vPanel.add(new HTML("Size : " + albumModel.getAlbumSize()));
			
			imagePanelList.add(vPanel);
		}
		return imagePanelList;
	}
	
	private List<Widget> createPhotoImagePanelList(List<PhotoModel> photoList){
		List<Widget> imagePanelList = new ArrayList<Widget>();
		PhotoModel photoModel = null;
		for(int i=0;i<photoList.size();i++){
			photoModel = photoList.get(i);
			
			Image fullImage = new Image(photoModel.getUrl());
			final PopupPanel imagePopup = new PopupPanel(true);
			imagePopup.setAnimationEnabled(true);
			imagePopup.setWidget(fullImage);
			
			fullImage.addClickHandler(new ClickHandler() {
			      public void onClick(ClickEvent event) {
			        imagePopup.hide();
			      }
			});
			
			Image image = new Image();
			image.setUrl(photoModel.getThumbUrl());
			image.setHeight("100");
			image.setWidth("100");
			image.addClickHandler(new ClickHandler() {
			    public void onClick(ClickEvent event) {
			      imagePopup.center();
			    }
			});
			

			VerticalPanel  vPanel  = new VerticalPanel ();
			vPanel.add(image);
			
			vPanel.add(new HTML(photoModel.getPhotoTitle()));
			
			imagePanelList.add(vPanel);
		}
		return imagePanelList;
	}
	
	private Widget createGrid(List<Widget> panelList) {
		// Create a grid
		int col = 4;
		int row = panelList.size()/4 + 1;
		Grid grid = new Grid(row,col);
		
		//imagePopup.setWidget();
		for(int i=0;i<panelList.size();i++){
			grid.setWidget(i%col , i/col, panelList.get(i));		
		}

		return grid;
	}	
}
